package com.dlust.notestxx;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.dlust.notestxx.Adapter.NoteListAdapter;
import com.dlust.notestxx.Interface.NotesClickListener;
import com.dlust.notestxx.Model.Note;
import com.dlust.notestxx.Model.RoomDb;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    RecyclerView recyclerView;
    NoteListAdapter noteListAdapter;
    RoomDb database;
    List<Note> notes = new ArrayList<>();
    FloatingActionButton fabBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerView = findViewById(R.id.noteRv);
        fabBtn = findViewById(R.id.addBtn);
        database = RoomDb.getInstance(this);
        notes = database.mainDAO().getAll();
        updateRecycle(notes);
        fabBtn.setOnClickListener(v -> {
            Intent intent = new Intent(MainActivity.this, NotesTakeActivity.class);
            startActivityForResult(intent, 101);
        });
    }

    @SuppressLint("NotifyDataSetChanged")
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==101) {
            if(resultCode == Activity.RESULT_OK) {
                assert data != null;
                Note note = (Note) data.getSerializableExtra("note");
                database.mainDAO().insert(note);
                notes.clear();
                notes.addAll(database.mainDAO().getAll());
                noteListAdapter.notifyDataSetChanged();
            }
        }else if(requestCode==102) {
            if(resultCode == Activity.RESULT_OK) {
                assert data != null;
                Note note = (Note) data.getSerializableExtra("note");
                database.mainDAO().Update(note);
                notes.clear();
                notes.addAll(database.mainDAO().getAll());
                noteListAdapter.notifyDataSetChanged();
            }
        }
    }

    private void updateRecycle(List<Note> notes) {
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, LinearLayoutManager.VERTICAL));
        noteListAdapter = new NoteListAdapter(MainActivity.this, notes, notesClickListener);
        recyclerView.setAdapter(noteListAdapter);
    }

    private final NotesClickListener notesClickListener = new NotesClickListener() {
        @Override
        public void onClick(Note note) {
            Intent intent = new Intent(MainActivity.this, NotesTakeActivity.class);
            intent.putExtra("old_note", note);
            startActivityForResult(intent, 102);
        }

        @Override
        public void onLongClick(Note note, CardView cardView) {

        }
    };
}